﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Web;
using System.Web.SessionState;
using DTcms.Web.UI;
using DTcms.Common;

namespace DTcms.Web.m
{
    /// <summary>
    /// qiugou_detail 的摘要说明
    /// </summary>
    public class xunjia_check : IHttpHandler, IRequiresSessionState
    {
        protected int id = 0;
        public void ProcessRequest(HttpContext context)
        {
            id = DTRequest.GetQueryInt("id", 0);
            if (id == 0)
            {
                return;
            }
            //检查用户是否登录
            string user_name = DTRequest.GetFormString("txtUserName");
            string user_pwd = DTRequest.GetFormString("txtPassword");
            Model.users uModel = new BLL.users().GetModel(user_name, user_pwd, 0, 1, true);
            BLL.users blluser = new BLL.users();
            BLL.xunjia bll = new BLL.xunjia();
            
            Model.xunjia model = bll.GetModel(id);
            //#####根据用户会员级别查看会员委托求购信息详情电话的查看次数
            //##### 2018 11 01
            BLL.user_groups bllgroup = new BLL.user_groups();

            string tips = "";
            string mobile = "";
            StringBuilder strTxt = new StringBuilder();
            strTxt.Append("{");
        
            //strTxt.Append(",\"umodel\":" + uModel.user_name+","+uModel.group_id);
            if (model.user_id > 0)
            {
                Model.users userModel = new BLL.users().GetModel(model.user_id);
                if (userModel != null)
                {
                  
                   
                    if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_pwd) || uModel == null || uModel.group_id < 2)
                    {
                        strTxt.Append("\"mobile\":\"请联系客服开通VIP\"");
                        mobile += "未登录或普通会员\"mobile\":\"请联系客服开通VIP\"";
                    }
                    else
                    {
                        #region 委托求购添加查看次数
                        int userViewCount = bllgroup.GetGongqiuView(uModel.group_id);
                        int group_1view = bllgroup.GetGongqiuView(2);
                        int group_2view = bllgroup.GetGongqiuView(3);
                        if (group_1view == 0 ) { group_1view = 1; }
                        if (group_2view == 0) { group_2view = 3; }
                        BLL.view_log viewbll = new BLL.view_log();


                        //###认证会员 只能查看一次 
                        //###钻石会员 钻石 3条
                        //### Demon 2018-10-30
                        #region old 查看逻辑
                        //if (uModel.group_id > 1)
                        //{
                        //    if (uModel.readtime != null)
                        //    {
                        //        if (uModel.readtime.Date == DateTime.Now.Date)
                        //        {
                        //            if (uModel.readcount >= userViewCount)//认证会员 只能查看一次 
                        //            {
                        //                strTxt.Append("\"mobile\":\"请联系客服开通VIP\"");
                        //                if (uModel.group_id == 2)
                        //                    tips = "认证会员活动期可以体验每天查看" + group_1view + "次的权利,升级VIP 可以每天查看" + group_2view + "次,赶紧联系客服升级吧";
                        //            }
                        //            else
                        //            {
                        //                if (bll.CheckPhoneNum(id) == "error")
                        //                {
                        //                    strTxt.Append("\"mobile\":\"请联系客服开通VIP\"");
                        //                    tips = "该条信息已经被访问超过三十次,拒绝访问!";
                        //                }
                        //                else
                        //                {
                        //                    strTxt.Append("\"mobile\":\"" + userModel.mobile + "\"");
                        //                    blluser.UpdateField(uModel.id, "readcount=readcount+1");
                        //                    viewbll.AddLog(1, uModel.id, uModel.user_name, model.id, model.remark, "从xunjia_check中查看", DateTime.Now);
                        //                }
                        //                //Utils.WriteTextLog("xunjia_check-" + id, "用户" + user_name + "查看该条信息；" + tips, DateTime.Now);

                        //            }
                        //        }
                        //        else
                        //        {
                        //            if (bll.CheckPhoneNum(id) == "error")
                        //            {
                        //                strTxt.Append("\"mobile\":\"请联系客服开通VIP\"");
                        //                tips = "该条信息已经被访问超过三十次,拒绝访问!";
                        //            }
                        //            else
                        //            {
                        //                strTxt.Append("\"mobile\":\"" + userModel.mobile + "\"");
                        //                blluser.UpdateField(uModel.id, "readcount=1,readtime=getdate()");
                        //                viewbll.AddLog(1, uModel.id, uModel.user_name, model.id, model.remark, "从xunjia_check中查看", DateTime.Now);
                        //            }
                        //            //Utils.WriteTextLog("xunjia_check-" + id, "用户" + user_name + "查看该条信息；" + tips, DateTime.Now);
                        //        }
                        //    }
                        //    else
                        //    {
                        //        if (bll.CheckPhoneNum(id) == "error")
                        //        {
                        //            strTxt.Append("\"mobile\":\"请联系客服开通VIP\"");
                        //            tips = "该条信息已经被访问超过三十次,拒绝访问!";
                        //        }
                        //        else
                        //        {
                        //            strTxt.Append("\"mobile\":\"" + userModel.mobile + "\"");
                        //            blluser.UpdateField(uModel.id, "readcount=1,readtime=getdate()");
                        //            viewbll.AddLog(1, uModel.id, uModel.user_name, model.id, model.remark, "从xunjia_check中查看", DateTime.Now);
                        //        }
                        //        //Utils.WriteTextLog("xunjia_check-" + id, "用户" + user_name + "查看该条信息；" + tips, DateTime.Now);
                        //    }
                        //}
                        #endregion


                        #region  new 查看逻辑 认证会员总共查看  VIP会员注册日期2019-05-01之前的原模式 之后的 总共查看
                        string msg = "";
                        if (uModel.group_id > 1)
                        {
                            /// VIP 并且 注册日期是在 2019-05-01 之前的执行原来的逻辑
                            #region VIP 并且 注册日期是在 2019-05-01 之前的执行原来的逻辑
                            if (uModel.group_id == 3 && uModel.upgrade_time < DateTime.Parse("2019-05-01 00:00:00"))
                            {
                                if (uModel.readtime != null)
                                {
                                    if (uModel.readtime.Date == DateTime.Now.Date)
                                    {
                                        if (uModel.readcount >= 3)//认证会员 只能查看一次 
                                        {
                                            strTxt.Append("\"mobile\":\"请联系客服开通VIP\"");
                                            mobile += "每天次数超过限制\"mobile\":\"请联系客服开通VIP\"";
                                            if (uModel.group_id == 2)
                                                tips = "认证会员活动期可以体验每天查看" + group_1view + "次的权利,升级VIP 可以每天查看" + group_2view + "次,赶紧联系客服升级吧";
                                        }
                                        else
                                        {
                                            if (bll.CheckPhoneNum(id) == "error")
                                            {
                                                strTxt.Append("\"mobile\":\"请联系客服开通VIP\"");
                                                mobile += "该条信息已经被访问超过三十次,拒绝访问!\"mobile\":\"请联系客服开通VIP\"";
                                                tips = "该条信息已经被访问超过三十次,拒绝访问!";
                                            }
                                            else
                                            {
                                                strTxt.Append("\"mobile\":\"" + userModel.mobile + "\"");
                                                mobile += "\"mobile\":\"" + userModel.mobile + "\"";
                                               
                                                if (!viewbll.Exists(uModel.id, model.id)) { blluser.UpdateField(uModel.id, "readcount=readcount+1"); }
                                                else { msg = "重复查看，不计次数"; }
                                              
                                                viewbll.AddLog(1, uModel.id, uModel.user_name, model.id, model.remark, "从xunjia_check中查看"+ msg, DateTime.Now);
                                            }
                                            //Utils.WriteTextLog("xunjia_check-" + id, "用户" + user_name + "查看该条信息；" + tips, DateTime.Now);

                                        }
                                    }
                                    else
                                    {
                                        if (bll.CheckPhoneNum(id) == "error")
                                        {
                                            strTxt.Append("\"mobile\":\"请联系客服开通VIP\"");
                                            mobile += "该条信息已经被访问超过三十次,拒绝访问!\"mobile\":\"请联系客服开通VIP\"";
                                            tips = "该条信息已经被访问超过三十次,拒绝访问!";
                                        }
                                        else
                                        {
                                            strTxt.Append("\"mobile\":\"" + userModel.mobile + "\"");

                                            if (!viewbll.Exists(uModel.id, model.id)) { blluser.UpdateField(uModel.id, "readcount=1,readtime=getdate()"); }
                                            else { msg = "重复查看，不计次数"; }
                                            
                                            viewbll.AddLog(1, uModel.id, uModel.user_name, model.id, model.remark, "从xunjia_check中查看"+ msg, DateTime.Now);
                                        }
                                        //Utils.WriteTextLog("xunjia_check-" + id, "用户" + user_name + "查看该条信息；" + tips, DateTime.Now);
                                    }
                                }
                                else
                                {
                                    if (bll.CheckPhoneNum(id) == "error")
                                    {
                                        strTxt.Append("\"mobile\":\"请联系客服开通VIP\"");
                                        mobile += "该条信息已经被访问超过三十次,拒绝访问!\"mobile\":\"请联系客服开通VIP\"";
                                        tips = "该条信息已经被访问超过三十次,拒绝访问!";
                                    }
                                    else
                                    {
                                        strTxt.Append("\"mobile\":\"" + userModel.mobile + "\"");
                                        mobile += "\"mobile\":\"" + userModel.mobile + "\"";
                                        if (!viewbll.Exists(uModel.id, model.id)) { blluser.UpdateField(uModel.id, "readcount=1,readtime=getdate()"); }
                                        else { msg = "重复查看，不计次数"; }
                                       
                                        viewbll.AddLog(1, uModel.id, uModel.user_name, model.id, model.remark, "从xunjia_check中查看"+ msg, DateTime.Now);
                                    }
                                    //Utils.WriteTextLog("xunjia_check-" + id, "用户" + user_name + "查看该条信息；" + tips, DateTime.Now);
                                }
                            }
                            #endregion


                            #region 认证会员和注册日期在20190501之后的VIP
                            else
                            {
                                if (uModel.readcountTotal > userViewCount)
                                {
                                    strTxt.Append("\"mobile\":\"请联系客服开通VIP\"");
                                    mobile += "总次数超过限制\"mobile\":\"请联系客服开通VIP\"";
                                    tips = "认证会员活动期可以体验查看" + group_1view + "次的权利,升级VIP 可以查看" + group_2view + "次,赶紧联系客服升级吧";
                                }
                                else
                                {
                                    if (bll.CheckPhoneNum(id) == "error")
                                    {
                                        strTxt.Append("\"mobile\":\"请联系客服开通VIP\"");
                                        mobile += "该条信息已经被访问超过三十次,拒绝访问!\"mobile\":\"请联系客服开通VIP\"";
                                        tips = "该条信息已经被访问超过三十次,拒绝访问!";
                                    }
                                    else
                                    {
                                        strTxt.Append("\"mobile\":\"" + userModel.mobile + "\"");
                                        mobile += "\"mobile\":\"" + userModel.mobile + "\"";
                                        tips = "您还有" + (userViewCount - uModel.readcountTotal) + "次查看机会可用！";
                                        if (!viewbll.Exists(uModel.id, model.id)) { blluser.UpdateField(uModel.id, "readcount=readcount+1,readcountTotal=readcountTotal+1,readtime=getdate()"); }
                                        else { msg = "重复查看，不计次数"; tips = ""; }
                                       
                                        viewbll.AddLog(1, uModel.id, uModel.user_name, model.id, model.remark, "从xunjia_check中查看"+ msg, DateTime.Now);
                                    }
                                }
                            }
                            #endregion


                        }
                        #endregion

                        #endregion

                        Utils.WriteTextLog("xunjia_check-" + uModel.mobile, "询价ID" + id + "用户" + user_name +mobile + "查看该条信息；" + tips, DateTime.Now);

                    }
                }
                else{strTxt.Append("\"mobile\":\"\""); }
            }
            else{ strTxt.Append("\"mobile\":\"\"");}
            strTxt.Append(",\"tips\":\"" + tips + "\"");
            if (!string.IsNullOrEmpty(user_name) && !string.IsNullOrEmpty(user_pwd) & uModel != null)
            {
                strTxt.Append(",\"cur_group_id\":" + uModel.group_id);
            }
            else
            {
                strTxt.Append(",\"cur_group_id\":0");
            }
          

           
            strTxt.Append("}");
            context.Response.Write(strTxt.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}